package com.bit.strongbaby;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * GraphViewDemo creates some dummy data to demonstrate the GraphView component.
 * 
 * @author Arno den Hond
 * 
 */
public class GraphMainActivity extends Activity {
	Button btn1;
	WordDBHelper2 mHelper;
	SQLiteDatabase db;
	float mon;
	//int mon;
	float key=(float) 59.1;
	
	@SuppressWarnings("null")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_graph);
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd");
		String datetime1 = sdf1.format(cal.getTime());
		
		mHelper = new WordDBHelper2(this); 
		db = mHelper.getWritableDatabase();
		
//		float key = KeyMeasureActivity.key_re_b;
//		int c = (int)key;
		
		mon = (float)(MenuActivity.day);
		int a = (int) mon;
		
//		String b = Integer.toString(c);
		
		Intent key_ini = getIntent();							// 키값 intent 
		String key_String = key_ini.getStringExtra("key");		// 키값 넘겨 받기	
		key = Float.parseFloat(key_String);

		btn1 = (Button) findViewById(R.id.mainkey);
		btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getBaseContext(), MenuActivity.class);
				startActivity(intent);
			}
		});

//		// 평균 키
		float[] valmah = new float[] { 55.2f, 59.0f, 62.5f, 65.2f, 66.8f,
				69.0f, 70.4f, 71.9f, 73.5f, 74.6f, 76.5f, 77.8f };
		float[] valwah = new float[] { 54.2f, 58.0f, 61.1f, 63.8f, 65.7f,
				67.5f, 69.1f, 70.5f, 72.2f, 73.5f, 75.6f, 76.9f };
//		 float[] valbh = new float[] { 50.0f, 50.0f, 50.0f, 70.0f, 50.0f,
//		 50.0f, 50.0f, 50.0f, 50.0f, 50.0f, 50.0f, 50.0f };
//		 String[] valbt = new String[] { "", "", "", "", "", "", "", "", "",
//		 "", "", ""};
		 
		 if(1<a&&a<=30){
			db.execSQL("UPDATE baby_grow SET height = '"+key+"', date='"+datetime1+"' WHERE month = 1;");
		}else if(30<a&&a<=60){
			db.execSQL("UPDATE baby_grow SET height = "+key+", date='"+datetime1+"' WHERE month = 2;");
		}else if(60<a&&a<=90){
			db.execSQL("UPDATE baby_grow SET height = '"+key+"', date='"+datetime1+"' WHERE month = 3;");
		}else{
			db.execSQL("UPDATE baby_grow SET height = '"+key+"', date='"+datetime1+"' WHERE month = 12;");
		}
	
		int i=0, j=0;
		float[] valbh ;
		valbh = new float[12];
		String[] valbt ;
		valbt = new String[12];
		
//		while (cursor.moveToNext()) {
//			for(i=0;i<12;i++){
//				valbh[i] = cursor.getFloat(0);
//				//Log.d("!", cursor.getFloat(i));
//				valbt[i] = cursor1.getString(2);}}
			
		Cursor cur;
		Cursor cur1;
		cur = db.rawQuery("SELECT height FROM baby_grow", null);
		cur1 = db.rawQuery("SELECT date FROM baby_grow", null);
		while (cur.moveToNext()) {
			valbh[i] = cur.getFloat(0);
			i++;
		}while (cur1.moveToNext()) {
			valbt[j]= cur1.getString(0);
			j++;
		}
		Log.d("욧욧0",valbt[0]+"");
		Log.d("욧욧1",valbt[1]+"");
		Log.d("욧욧2",valbt[2]+"");
		Log.d("욧욧3",valbt[3]+"");		
		cur.close();
		cur1.close();
		mHelper.close();
		
		String[] verlabels = new String[] { "80cm", "", "", "", "", "75cm", "",
				"", "", "", "70cm", "", "", "", "", "65cm", "", "", "", "",
				"60cm", "", "", "", "", "55cm", "", "", "", "", "50cm" };
		String[] horlabels = new String[] { "0M", "1M", "2M", "3M", "4M", "5M",
				"6M", "7M", "8M", "9M", "10M", "11M", "12M" };
		GraphActivity graphView = (GraphActivity) this
				.findViewById(R.id.drawview);
		graphView.initGraph(valmah, valwah, valbh, valbt, "", horlabels,
			verlabels, GraphActivity.LINE);

	}
}